package cn.ztydata.teachingsystem.heaven.web.controller;

import cn.ztydata.teachingsystem.common.Page;
import cn.ztydata.teachingsystem.heaven.cons.MsgTemplate;
import cn.ztydata.teachingsystem.heaven.entity.Daily;
import cn.ztydata.teachingsystem.heaven.entity.DailyComment;
import cn.ztydata.teachingsystem.heaven.service.DailyCommentService;
import cn.ztydata.teachingsystem.heaven.web.dto.SpecialtyGroup.DailyCommentDto;
import cn.ztydata.teachingsystem.webservice.result.json.JsonResult;
import cn.ztydata.teachingsystem.webservice.result.json.JsonResultPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import java.text.MessageFormat;

/**
 * DailyCommentController
 *
 * @author lyj
 * @since 2015/5/7
 */
@RequestMapping(value = "/dailyReply")
@Controller
public class DailyCommentController extends BaseController {
    @Autowired
    private DailyCommentService dailyCommentService;


    //<editor-fold desc="新增">
    /**
     * 新增教学日志评论
     *
     * @param dailyCommentDto
     * @return JsonResult
     * @author lyj
     * @since 2015-5-6
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult addDailyComment(@RequestBody DailyCommentDto dailyCommentDto) {
        Assert.notNull(dailyCommentDto.getDailyId(), MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "日志ID"));
        Assert.notNull(dailyCommentDto.getReplyId(), MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "日志ID"));

        dailyCommentService.addDailyComment(mapper.map(dailyCommentDto, DailyComment.class));

        log.debug("新增教学日志评论");
        return new JsonResult(true, "新增日志成功！");
    }
    //</editor-fold>


    //<editor-fold desc="删除">

    /**
     * 删除评论（回复）
     *
     * @param commentId 回复ID
     * @param dailyId 日志Id
     * @return JsonResult
     * @author lyj
     * @since 2015-5-6
     */
    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    @ResponseBody
    public JsonResult deleteById(@RequestParam("replyId") long commentId,
                                 @RequestParam("dailyId") long dailyId) {
        Assert.isTrue(commentId > 0, MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "回复ID"));
        Assert.isTrue(dailyId > 0, MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "日志ID"));

        dailyCommentService.deleteById(commentId, dailyId);

        log.debug("删除评论（回复）");
        return new JsonResult(true, "删除日志成功！");
    }
    //</editor-fold>



    //<editor-fold desc="查询">
    /**
     * 根据日志ID获取回复列表
     *
     * @param dailyId 日志ID
     * @param pageNum 当前页码
     * @param pageSize 每页条数
     * @return JsonResultPage
     * @author lyj
     * @since 2015-5-6
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public JsonResultPage getAllComment(@RequestParam("id") long dailyId,
                                        @RequestParam("page") int pageNum,
                                        @RequestParam("pageSize") int pageSize) {
        Assert.isTrue(dailyId > 0, MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "日志ID"));
        Assert.isTrue(pageNum > 0, MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "当前页码"));
        Assert.isTrue(pageSize > 0, MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "每页条数"));

        Page<DailyComment> page = dailyCommentService.getAllComment(dailyId, pageNum, pageSize);

        log.debug("根据日志ID获取回复列表");
        return new JsonResultPage(new Page(page.getTotal(), pageSize, pageNum, mapper.mapList(page.getItems(), DailyCommentDto.class)));
    }
    //</editor-fold>
}
